<?php
/* MrT function to initial a new db object*/
function mrt_init_new_db_obj() {
    $newdb = new wpdb('root', 'root', 'test', 'localhost');
    $newdb->show_errors();
//    $myrows = $newdb->get_results( "SELECT test_id FROM test1" );
//foreach ( (array) $myrows as $o ) {
//			var_dump($o->test_id);
//		}
    return $newdb;
}
//mrt_show_law_docs();

	function custom_excerpt($text, $chars) 
	{
		$text = strip_shortcodes($text);
		$text = str_replace(']]>', ']]&gt;', $text);
		$text = strip_tags($text);
		$text = substr($text,0,$chars);
		$text = substr($text,0,strrpos($text,' '));
		$text = $text . " [...]";
		echo $text;
	}
	
	add_filter('the_content', 'my_excerpts');
	
	function my_excerpts($content = false) 
	{
	// If is the home page, an archive, or search results
		if(is_front_page() || is_archive() || is_search()) :
			global $post;
			$content = $post->post_excerpt;
	
		// If an excerpt is set in the Optional Excerpt box
			if($content) :
				$content = apply_filters('the_excerpt', $content);
	
		// If no excerpt is set
			else :
				$content = $post->post_content;
				$excerpt_length = 500;
				$words = explode(' ', $content, $excerpt_length + 1);
				if(count($words) > $excerpt_length) :
					array_pop($words);
					array_push($words, '...');
					$content = implode(' ', $words);
				endif;
				$content = '<p>' . $content . '</p>';
	
			endif;
		endif;
	
	// Make sure to return the content
		return $content;
	}
	
	function get_category_name($cat_id)
	{
		$categories = get_categories();
		foreach( $categories as $category ) {
			if( $category->term_id == $cat_id ) {
				$cat_name = $category->name;
				break;
			}
		}
		
		return $cat_name;
	}

////////////////////////////////////////////////////////////////////////////////
// Random post
////////////////////////////////////////////////////////////////////////////////
function gte_random_posts (){
global $wpdb, $post;
$current_title = get_the_title();
$randompostthis = $wpdb->get_results("SELECT $wpdb->posts.ID, post_title, post_name, post_date, post_type, post_status FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND post_title != '$current_title' ORDER BY RAND() limit 10");
foreach ($randompostthis as $post) {
$post_title = htmlspecialchars(stripslashes($post->post_title));
echo "<li><a href=\"".get_permalink()."\">$post_title</a></li>";
}
}

////////////////////////////////////////////////////////////////////////////////
// update post
////////////////////////////////////////////////////////////////////////////////
function gte_recent_updated_posts(){
global $wpdb, $post;

$recentupdatethis = $wpdb->get_results("SELECT $wpdb->posts.ID, post_title, post_name, post_date, post_type, post_status, post_modified FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER by post_modified_gmt DESC limit 10");
foreach ($recentupdatethis as $post) {
$post_title = htmlspecialchars(stripslashes($post->post_title));
echo "<li><a href=\"".get_permalink()."\">$post_title</a></li>";
}
}

////////////////////////////////////////////////////////////////////////////////
// Recent Comments
////////////////////////////////////////////////////////////////////////////////

function get_hottopics($limit = 12) {
    global $wpdb, $post;
    $mostcommenteds = $wpdb->get_results("SELECT  $wpdb->posts.ID, post_title, post_name, post_date, COUNT($wpdb->comments.comment_post_ID) AS 'comment_total' FROM $wpdb->posts LEFT JOIN $wpdb->comments ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID WHERE comment_approved = '1' AND post_date_gmt < '".gmdate("Y-m-d H:i:s")."' AND post_status = 'publish' AND post_password = '' GROUP BY $wpdb->comments.comment_post_ID ORDER  BY comment_total DESC LIMIT $limit");
    foreach ($mostcommenteds as $post) {
			$post_title = htmlspecialchars(stripslashes($post->post_title));
			$comment_total = (int) $post->comment_total;
			echo "<li><a href=\"".get_permalink()."\">$post_title&nbsp;<strong>($comment_total)</strong></a></li>";
    }
}


////////////////////////////////////////////////////////////////////////////////
// Recent Comments
////////////////////////////////////////////////////////////////////////////////
function mw_recent_comments(
	$no_comments = 10,
	$show_pass_post = false,
	$title_length = 50, 	// shortens the title if it is longer than this number of chars
	$author_length = 30,	// shortens the author if it is longer than this number of chars
	$wordwrap_length = 50, // adds a blank if word is longer than this number of chars
	$type = 'all', 	// Comments, trackbacks, or both?
	$format = '<li>%date%: <a href="%permalink%" title="%title%">%title%</a> (von %author_full%)</li>',
	$date_format = 'd.m.y, H:i',
	$none_found = '<li>Keine Kommentare vorhanden.</li>',	// None found
	$type_text_pingback = 'Pingback von',
	$type_text_trackback = 'Trackback von',
	$type_text_comment = 'von'

	) {

	//Language...
	$mwlang_anonymous = 'Anonym'; // Anonymous
	$mwlang_authorurl_title_before = 'Webseite von &lsaquo;';
	$mwlang_authorurl_title_after = '&rsaquo; besuchen';


    global $wpdb;

    $request = "SELECT ID, comment_ID, comment_content, comment_author, comment_author_url, comment_date, post_title, comment_type
				FROM $wpdb->comments LEFT JOIN $wpdb->posts ON $wpdb->posts.ID=$wpdb->comments.comment_post_ID
				WHERE post_status IN ('publish','static')";

	switch($type) {
		case 'all':
			// add nothing
			break;
		case 'comment_only':
			//
			$request .= "AND $wpdb->comments.comment_type='' ";
			break;
		case 'trackback_only':
			$request .= "AND ( $wpdb->comments.comment_type='trackback' OR $wpdb->comments.comment_type='pingback' ) ";
			break;
	 default:
 		//
			break;

	}

	if (!$show_pass_post) $request .= "AND post_password ='' ";

	$request .= "AND comment_approved = '1' ORDER BY comment_ID DESC LIMIT $no_comments";

	$comments = $wpdb->get_results($request);
    $output = '';
	if ($comments) {
    	foreach ($comments as $comment) {

			// Permalink to post/comment
			$loop_res['permalink'] = get_permalink($comment->ID). '#comment-' . $comment->comment_ID;

			// Title of the post
			$loop_res['post_title'] = stripslashes($comment->post_title);
			$loop_res['post_title'] = wordwrap($loop_res['post_title'], $wordwrap_length, ' ' , 1);

			if (strlen($loop_res['post_title']) >= $title_length) {
				$loop_res['post_title'] = substr($loop_res['post_title'], 0, $title_length) . '&#8230;';
			}

			// Author's name only
        	$loop_res['author_name'] = stripslashes($comment->comment_author);
			$loop_res['author_name'] = wordwrap($loop_res['author_name'], $wordwrap_length, ' ' , 1);

			if ($loop_res['author_name'] == '') $loop_res['author_name'] = $mwlang_anonymous;
			if (strlen($loop_res['author_name']) >= $author_length) {
				$loop_res['author_name'] = substr($loop_res['author_name'], 0, $author_length) . '&#8230;';
			}

			// Full author (link, name)
			$author_url = $comment->comment_author_url;
			if (empty($author_url)) {
				$loop_res['author_full'] = $loop_res['author_name'];
			} else {
				$loop_res['author_full'] = '<a href="' . $author_url . '" title="' . $mwlang_authorurl_title_before . $loop_res['author_name'] . $mwlang_authorurl_title_after . '">' . $loop_res['author_name'] . '</a>';
			}

/*
			// Comment excerpt
			$comment_excerpt = strip_tags($comment->comment_content);
			$comment_excerpt = stripslashes($comment_excerpt);
			if (strlen($comment_excerpt) >= $comment_length) {
				$comment_excerpt = substr($comment_excerpt, 0, $comment_length) . '...';
			}

*/

			// Comment type
			if ( $comment->comment_type == 'pingback' ) {
				$loop_res['comment_type'] = $type_text_pingback;
			} elseif ( $comment->comment_type == 'trackback' ) {
				$loop_res['comment_type'] = $type_text_trackback;
			} else {
				$loop_res['comment_type'] = $type_text_comment;
			}

			// Date of comment
			$loop_res['comment_date'] = mysql2date($date_format, $comment->comment_date);

			// Output element
			$element_loop = str_replace('%permalink%', $loop_res['permalink'], $format);
			$element_loop = str_replace('%title%', $loop_res['post_title'], $element_loop);
			$element_loop = str_replace('%author_name%', $loop_res['author_name'], $element_loop);
			$element_loop = str_replace('%author_full%', $loop_res['author_full'], $element_loop);
			$element_loop = str_replace('%date%', $loop_res['comment_date'], $element_loop);
			$element_loop = str_replace('%type%', $loop_res['comment_type'], $element_loop);


			$output .= $element_loop . "\n";


		} //foreach

		$output = convert_smilies($output);

	} else {
		$output .= $none_found;
    }

    echo $output;
}

////////////////////////////////////////////////////////////////////////////////
// excerpt features
////////////////////////////////////////////////////////////////////////////////

function the_excerpt_feature($excerpt_length=35, $allowedtags='', $filter_type='none', $use_more_link=true, $more_link_text="...read more", $force_more_link=true, $fakeit=1, $fix_tags=true) {
	if (preg_match('%^content($|_rss)|^excerpt($|_rss)%', $filter_type)) {
		$filter_type = 'the_' . $filter_type;
	}
	$text = apply_filters($filter_type, get_the_excerpt_feature($excerpt_length, $allowedtags, $use_more_link, $more_link_text, $force_more_link, $fakeit));
	$text = ($fix_tags) ? balanceTags($text) : $text;
	echo $text;
}

function get_the_excerpt_feature($excerpt_length, $allowedtags, $use_more_link, $more_link_text, $force_more_link, $fakeit) {
	global $id, $post;
	$output = '';
	$output = $post->post_excerpt;
	if (!empty($post->post_password)) { // if there's a password
		if ($_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password) {  // and it doesn't match the cookie
			$output = __('There is no excerpt because this is a protected post.');
			return $output;
		}
	}

	// If we haven't got an excerpt, make one.
	if ((($output == '') && ($fakeit == 1)) || ($fakeit == 2)) {
		$output = $post->post_content;
		$output = strip_tags($output, $allowedtags);
		$blah = explode(' ', $output);
		if (count($blah) > $excerpt_length) {
			$k = $excerpt_length;
			$use_dotdotdot = 1;
		} else {
			$k = count($blah);
			$use_dotdotdot = 0;
		}
		$excerpt = '';
		for ($i=0; $i<$k; $i++) {
			$excerpt .= $blah[$i] . ' ';
		}
		// Display "more" link (use css class 'more-link' to set layout).
		if (($use_more_link && $use_dotdotdot) || $force_more_link) {
			$excerpt .= "<a href=\"". get_permalink() . "#more-$id\">$more_link_text</a>";
		} else {
			$excerpt .= ($use_dotdotdot) ? '...' : '';
		}
		 $output = $excerpt;
	} // end if no excerpt
	return $output;
}
////////////////////////////////////////////////////////////////////////////////
// end
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////
//widget
///////////////////////////////
if ( function_exists('register_sidebar') ) 
{
	register_sidebar(array(
		'name'=>'Sidebar Widget',
		'before_widget' => '<div class="sidebar_box">',
		'after_widget' => '</div></div>',
		'before_title' => '<h3>',
		'after_title' => '</h3><div class="sidebar_meta">'
	));
	
	register_sidebar(array(
		'name'=>'Footer 3 Max Widget',
		'before_widget' => '<div class="sidebar_box">',
		'after_widget' => '</div></div>',
		'before_title' => '<h3>',
		'after_title' => '</h3><div class="sidebar_meta">'
	));
	
	function unregister_problem_widgets() 
	{
		unregister_sidebar_widget('Calendar');
		unregister_sidebar_widget('Search');
		unregister_sidebar_widget('Categories');
	}
	
	add_action('widgets_init','unregister_problem_widgets');
}

function widget_about() 
{
?>
	<div class="sidebar_box">
        <h3>About Author</h3>
        <div class="sidebar_meta">
			<?php echo get_option('ibiz_mag_about_us'); ?>
        </div>
      </div>
<?php
}
if ( function_exists('register_sidebar_widget') )
    register_sidebar_widget(__('About Us'), 'widget_about');

function widget_archives() 
{
?>
	<div class="sidebar_box">
      <h3>Archives</h3>
      <div class="sidebar_meta">
      <ul>
          <?php wp_get_archives('type=monthly&limit=12&show_post_count=0'); ?>
      </ul>
      </div>
    </div>
<?php
}
if ( function_exists('register_sidebar_widget') )
    register_sidebar_widget(__('Archives'), 'widget_archives');

function widget_categories() 
{
?>
	<div class="sidebar_box">
      <h3>Topics</h3>
      <div class="sidebar_meta">
      <ul>
          <?php wp_list_categories('orderby=id&show_count=0&use_desc_for_title=0&title_li='); ?>
        </ul>
      </div>
    </div>
<?php
}
if ( function_exists('register_sidebar_widget') )
    register_sidebar_widget(__('Topics'), 'widget_categories');

function widget_rentries() 
{
?>
	<div class="sidebar_box">
      <h3>Recent Entries</h3>
      <div class="sidebar_meta">
      <ul>
          <?php get_archives('postbypost', 10); ?>
        </ul>
      </div>
    </div>
<?php
}
if ( function_exists('register_sidebar_widget') )
    register_sidebar_widget(__('Recent Entries'), 'widget_rentries');

function widget_rcomments() 
{
?>
	<div class="sidebar_box">
      <h3>Recent Comments</h3>
      <div class="sidebar_meta">
      <ul>
        <?php if(function_exists("get_recent_comments")) : ?>
        <?php get_recent_comments(); ?>
        <?php else : ?>
        <?php mw_recent_comments(7, false, 35, 15, 35, 'all', '<li><a href="%permalink%" title="%title%"><strong>%author_name%</strong> in %title%</a></li>','d.m.y, H:i'); ?>
        <?php endif; ?>
      </ul>
      </div>
    </div>
<?php
}
if ( function_exists('register_sidebar_widget') )
    register_sidebar_widget(__('Recent Comments'), 'widget_rcomments');

function widget_pages() 
{
?>
	<div class="sidebar_box">
      <h3>Pages</h3>
      <div class="sidebar_meta">
      <ul>
        <li><a href="<?php bloginfo('url'); ?>" title="Home">Home</a></li>
        <?php wp_list_pages('title_li=&depth=1'); ?>
      </ul>
      </div>
    </div>
<?php
}
if ( function_exists('register_sidebar_widget') )
    register_sidebar_widget(__('Pages'), 'widget_pages');

function widget_blogroll() 
{
?>
	<div class="sidebar_box">
      <h3>Blogroll</h3>
      <div class="sidebar_meta">
      <ul>
        <?php  
        get_links(-1, '<li>', '</li>', '', TRUE, 'url', FALSE);
      ?>
      </ul>
      </div>
    </div>
<?php
}
if ( function_exists('register_sidebar_widget') )
    register_sidebar_widget(__('Blogroll'), 'widget_blogroll');

function widget_mpp() 
{
?>
	<?php if(function_exists("akpc_most_popular")) : ?>
    <div class="sidebar_box">
      <h3>Most Popular Posts</h3>
      <div class="sidebar_meta">
          <ul>
            <?php akpc_most_popular(10); ?>
          </ul>
      </div>
    </div>
    <?php endif; ?>
<?php
}
if ( function_exists('register_sidebar_widget') )
    register_sidebar_widget(__('Most Popular Posts'), 'widget_mpp');
////////////////////////////////////////////////////////////////////////////////
//thumbnail image upload box
////////////////////////////////////////////////////////////
// This function tells WP to add a new "meta box"
function add_upload_thumbnail() 
{
	add_meta_box(
		'upload_thumbnail', // id of the <div> we'll add
		'Post Properties (iBizPress Magazine)', //title
		'add_upload_thumbnail_content', // callback function that will echo the box content
		'post', // where to add the box: on "post", "page", or "link" page
		'normal',
		'high' 
	);
}
// This function echoes the content of our meta box
function add_upload_thumbnail_content() 
{
	$post_id = $_GET['post'];
	$thumbnail = get_post_meta($post_id, "ibizmag_postimg_100px", true);
	$thumbnail_featured = get_post_meta($post_id, "ibizmag_postimg_184px", true);
	$youtube_id = get_post_meta($post_id, "ibizmag_youtube_id", true);
	
	echo 
		'<strong>1. Select a Post Thumbnail (100px by 100px) image for this post</strong><br /><br>
			URL: <input type="text" name="thumbnail_text" id="thumbnail_text" size="50" value="' . $thumbnail . '" /><br>
			<label> 
		 		Upload File: <input type="file" name="thumbnail" id="thumbnail" />
		 	</label>
		';
	
	echo 
		'<br><br><strong>2. Select a Post Featured Thumbnail (184px by 135px) image for this post</strong><br /><br>
			URL: <input type="text" name="featured_thumbnail_text" id="featured_thumbnail_text" size="50" value="' . $thumbnail_featured . '" /><br>
			<label> 
		 		Upload File: <input type="file" name="featured_thumbnail" id="featured_thumbnail" />
		 	</label>
		 <script language="javascript">
		 	document.getElementById("post").enctype = "multipart/form-data";
		 </script>
		';
	
	echo 
		'<br><br><strong>3. Youtube Video ID(video post)</strong><br /><br />
			<label> 
		 		<input type="text" name="youtube_id" id="youtube_id" value="' . $youtube_id . '" />
		 	</label><br><br>
			Example: http://www.youtube.com/watch?v=<strong>bOeWz2k4tTI</strong><br><br><em>(Bolded text is what you need!)</em>
		';
}
//This function upload and saves featured thumbnail data
function thumbnail_save_postdata()
{
	global $wpdb;

	$overrides = array('test_form' => false);
	$file_100 = wp_handle_upload($_FILES['thumbnail'], $overrides);
	$file_184 = wp_handle_upload($_FILES['featured_thumbnail'], $overrides);
	
	$url_100 = (strlen($file_100['url']) == 0) ? $_POST['thumbnail_text'] : $file_100['url'];
	$url_184 = (strlen($file_184['url']) == 0) ? $_POST['featured_thumbnail_text'] : $file_184['url'];

	if(!empty($url_100))
	{
		$postID = $_POST['post_ID'];

		$sqlQuery = "DELETE FROM " . $wpdb->prefix . "postmeta WHERE post_id=$postID AND meta_key='ibizmag_postimg_100px';";
		$wpdb->query($sqlQuery);

		// adding record in the database
		$sqlQuery = "INSERT INTO " . 
		$wpdb->prefix . "postmeta(post_id, meta_key, meta_value) 
		VALUES($postID, 'ibizmag_postimg_100px', '$url_100')";
		$wpdb->query($sqlQuery);
	}
	
	if(!empty($url_184))
	{
		$postID = $_POST['post_ID'];

		$sqlQuery = "DELETE FROM " . $wpdb->prefix . "postmeta WHERE post_id=$postID AND meta_key='ibizmag_postimg_184px';";
		$wpdb->query($sqlQuery);

		// adding record in the database
		$sqlQuery = "INSERT INTO " . 
		$wpdb->prefix . "postmeta(post_id, meta_key, meta_value) 
		VALUES($postID, 'ibizmag_postimg_184px', '$url_184')";
		$wpdb->query($sqlQuery);
	}
	
	$postID = $_POST['post_ID'];
	
	//save youtube id
	$youtube_id = $_POST['youtube_id'];
	
	if(!empty($youtube_id))
	{
		//delete old data
		$sqlQuery = "DELETE FROM " . $wpdb->prefix . "postmeta WHERE post_id=$postID AND meta_key='ibizmag_youtube_id';";
		$wpdb->query($sqlQuery);
		
		// adding record in the database
		$sqlQuery = "INSERT INTO " . 
		$wpdb->prefix . "postmeta(post_id, meta_key, meta_value) 
		VALUES($postID, 'ibizmag_youtube_id', '$youtube_id')";
		$wpdb->query($sqlQuery);
	}
}
/* Use the admin_menu action to define the custom boxes */
add_action('admin_menu', 'add_upload_thumbnail');

/* Use the save_post action to do something with the data entered */
add_action('save_post', 'thumbnail_save_postdata');
/**************************
 * Begin mrT custom box
 */
function add_upload_mrt_documents() 
{
	add_meta_box(
		'div_upload_documents', // id of the <div> we'll add
		'Thông tin về văn bản', //title
		'add_upload_mrt_documents_content', // callback function that will echo the box content
		'vbthue', // where to add the box: on "post", "page", or "link" page
		'normal',
		'high' 
	);
}

/* function to add content for echo in admin page */
function add_upload_mrt_documents_content() {
    $post_id = $_GET['post'];
	$doc_type = get_post_meta($post_id, "mrt_doc_type", true);
        $doc_no = get_post_meta($post_id, "mrt_doc_no", true);
        $doc_date = get_post_meta($post_id, "mrt_doc_date", true);
        $doc_sender = get_post_meta($post_id, "mrt_doc_sender", true);
        $doc_receiver = get_post_meta($post_id, "mrt_doc_receiver", true);
        $doc_file = get_post_meta($post_id, "mrt_doc_file", true);
	
	echo '<br><br><strong>Loại văn bản</strong>
			<label><input type="text" name="doc_type" id="doc_type" value="' . $doc_type . '" /></label><br><br>
			Example: Công văn<br><br>';        
        echo '<br><br><strong>Số văn bản</strong>
			<label><input type="text" name="doc_no" id="doc_no" value="' . $doc_no . '" /></label><br><br>
			Example: 285/SCT-VP<br><br>';
        echo '<br><br><strong>Ngày ban hành</strong>
			<label><input type="text" name="doc_date" id="doc_date" value="' . $doc_date . '" /></label><br><br>
			Example: 285/SCT-VP<br><br>';
        echo '<br><br><strong>CQ gửi</strong>
			<label><input type="text" name="doc_sender" id="doc_sender" value="' . $doc_sender . '" /></label><br><br>
			Example: 285/SCT-VP<br><br>';
        echo '<br><br><strong>CQ nhận</strong>
			<label><input type="text" name="doc_receiver" id="doc_receiver" value="' . $doc_receiver . '" /></label><br><br>
			Example: 285/SCT-VP<br><br>';
        echo 
		'<strong>File văn bản</strong>
			URL: <input type="text" name="doc_file" id="doc_file" size="50" value="' . $doc_file . '" /><br>
			<label> 
		 		Upload File: <input type="file" name="up_doc_file" id="up_doc_file" />
		 	</label>
                        <script language="javascript">
		 	document.getElementById("post").enctype = "multipart/form-data";
		 </script>
		';
}
/*MrT: function to upload docs and save extra data*/
function add_upload_mrt_documents_save_postdata() {
    global $wpdb;
//    $newdb = mrt_init_new_db_obj();

	$overrides = array('test_form' => false);
	$file_100 = wp_handle_upload($_FILES['up_doc_file'], $overrides);
	
	$url_100 = (strlen($file_100['url']) == 0) ? $_POST['doc_file'] : $file_100['url'];

	if(!empty($url_100))
	{
		$postID = $_POST['post_ID'];

		$sqlQuery = "DELETE FROM " . $wpdb->prefix . "postmeta WHERE post_id=$postID AND meta_key='mrt_doc_file';";
		$wpdb->query($sqlQuery);

		// adding record in the database
		$sqlQuery = "INSERT INTO " . 
		$wpdb->prefix . "postmeta(post_id, meta_key, meta_value) 
		VALUES($postID, 'mrt_doc_file', '$url_100')";
		$wpdb->query($sqlQuery);
	}
	
	$postID = $_POST['post_ID'];
	// Other text input
	$doc_type = $_POST['doc_type'];
	if(!empty($doc_type)){
		//delete old data
		$sqlQuery = "DELETE FROM " . $wpdb->prefix . "postmeta WHERE post_id=$postID AND meta_key='mrt_doc_type';";
		$wpdb->query($sqlQuery);
		// adding record in the database
		$sqlQuery = "INSERT INTO " . 
		$wpdb->prefix . "postmeta(post_id, meta_key, meta_value) 
		VALUES($postID, 'mrt_doc_type', '$doc_type')";
		$wpdb->query($sqlQuery);
	}
        
        $doc_no = $_POST['doc_no'];
	if(!empty($doc_no)){
		//delete old data
		$sqlQuery = "DELETE FROM " . $wpdb->prefix . "postmeta WHERE post_id=$postID AND meta_key='mrt_doc_no';";
		$wpdb->query($sqlQuery);
		// adding record in the database
		$sqlQuery = "INSERT INTO " . 
		$wpdb->prefix . "postmeta(post_id, meta_key, meta_value) 
		VALUES($postID, 'mrt_doc_no', '$doc_no')";
		$wpdb->query($sqlQuery);
	}
        
        $doc_date = $_POST['doc_date'];
	if(!empty($doc_date)){
		//delete old data
		$sqlQuery = "DELETE FROM " . $wpdb->prefix . "postmeta WHERE post_id=$postID AND meta_key='mrt_doc_date';";
		$wpdb->query($sqlQuery);
		// adding record in the database
		$sqlQuery = "INSERT INTO " . 
		$wpdb->prefix . "postmeta(post_id, meta_key, meta_value) 
		VALUES($postID, 'mrt_doc_date', '$doc_date')";
		$wpdb->query($sqlQuery);
	}
        
        $doc_sender = $_POST['doc_sender'];
	if(!empty($doc_sender)){
		//delete old data
		$sqlQuery = "DELETE FROM " . $wpdb->prefix . "postmeta WHERE post_id=$postID AND meta_key='mrt_doc_sender';";
		$wpdb->query($sqlQuery);
		// adding record in the database
		$sqlQuery = "INSERT INTO " . 
		$wpdb->prefix . "postmeta(post_id, meta_key, meta_value) 
		VALUES($postID, 'mrt_doc_sender', '$doc_sender')";
		$wpdb->query($sqlQuery);
	}
        
        $doc_receiver = $_POST['doc_receiver'];
	if(!empty($doc_receiver)){
		//delete old data
		$sqlQuery = "DELETE FROM " . $wpdb->prefix . "postmeta WHERE post_id=$postID AND meta_key='mrt_doc_receiver';";
		$wpdb->query($sqlQuery);
		// adding record in the database
		$sqlQuery = "INSERT INTO " . 
		$wpdb->prefix . "postmeta(post_id, meta_key, meta_value) 
		VALUES($postID, 'mrt_doc_receiver', '$doc_receiver')";
		$wpdb->query($sqlQuery);
	}
}
/* MrT define the 2nd custom box for document files */
add_action('admin_menu', 'add_upload_mrt_documents');
/* MrT Use the default save_post action to do something with the data entered */
add_action('save_post', 'add_upload_mrt_documents_save_postdata');


function mrt_admin_menu_cb() {
    echo 'xxxxxx0';
}
function mrt_admin_menu() {
//    global $menu;
//	
//	$menu[] = array( '', 'read', 'separator-jigoshop', '', 'wp-menu-separator jigoshop' );
	
    add_menu_page('', 'Văn bản thuế TNCN', 'manage_options', 'edit.php?post_type=vbthue');
//    add_submenu_page('vbthue', 'page title', 'menu title', 'manage_options', 'post-new.php?post_type=vbthue'); 
}

/*Add admin menu*/
//add_action('admin_menu', 'mrt_admin_menu');
/* Add port type mrT */
add_action( 'init', 'create_post_type' );
function create_post_type() {
	register_post_type( 'vbthue',
		array(
			'labels' => array(
				'name' => __( 'Văn bản thuế posttype name' ),
				'singular_name' => __( 'Văn bản thuế posttype singular name' )
			),
		'public' => true,
		'has_archive' => true,
		)
	);
}


/*
 * End mrT
 */
/////////////////////////////////////////////////////////////////////////////////
//////////////////////////////
//option page
//////////////////////////////
add_action('admin_menu', 'my_theme_menu');

function my_theme_menu() 
{
	if(strlen(get_option('ibiz_mag_about_us')) == 0)
	{
		update_option('ibiz_mag_about_us', 'This is an area on your website where you can add text. This section can serve as an informative location on your website, where you can talk about your site.');
	}
	
	if(strlen(get_option('ibiz_mag_feedburner_id')) == 0)
	{
		update_option('ibiz_mag_feedburner_id', '2378372');
	}
	
	if(strlen(get_option('ibiz_mag_feedburner_url')) == 0)
	{
		update_option('ibiz_mag_feedburner_url', 'http://feeds.feedburner.com/Izwan00');
	}
	
	add_theme_page('iBizPress Magazine Settings', 'iBizPress Magazine Settings', 8, __FILE__, 'iBizPress_options');
}

function iBizPress_options() 
{
	if ( $_REQUEST['updated'] ) echo '<div id="message" class="updated fade"><p><strong>iBizPress Magazine settings saved.</strong></p></div>';
	
	?>
    <div class="wrap">
        <h2>iBizPress Magazine Settings</h2>
        
        <form method="post" action="options.php">
            <?php wp_nonce_field('update-options'); ?>
        
        <h3>Common Settings</h3>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">Feedburner ID</th>
                    <td><input type="text" name="ibiz_mag_feedburner_id" value="<?php echo get_option('ibiz_mag_feedburner_id'); ?>" /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">Feedburner URL</th>
                    <td><input type="text" name="ibiz_mag_feedburner_url" value="<?php echo get_option('ibiz_mag_feedburner_url'); ?>" /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">About Us</th>
                    <td><textarea name="ibiz_mag_about_us" id="ibiz_mag_about_us" cols="45" rows="5"><?php echo get_option('ibiz_mag_about_us'); ?></textarea></td>
                </tr>
            </table>
         
          <h3>Home Page Settings</h3>
          	<table class="form-table">
                <tr valign="top">
                    <th scope="row">Video Category</th>
                    <td>
                    	<select name="ibizmag_video_cat" id="ibizmag_video_cat"> 
                         <option value="0">[Select Category]</option> 
                         <?php 
						  $ibizmag_video_cat = get_option('ibizmag_video_cat');
                          
						  $categories = get_categories(''); 
                          foreach ($categories as $cat) {
						  	$term_id = $cat->term_id;
                            $option = '<option value="'.$term_id.'"';
							
							if($term_id == $ibizmag_video_cat)
							{
								$option .= ' selected>';
							}
							else
							{
								$option .= '>';
							}
	
                            $option .= $cat->cat_name;
                            $option .= '</option>';
                            echo $option;
                          }
                         ?>
                        </select>
                    </td>
                </tr>
            </table>
            
            <h4>Featured Posts</h4>
            <table class="form-table">
            	
                <tr valign="top">
                    <th scope="row">Featured Post #1</th>
                    <td>
                    	<?php 
						global $post;
						$myposts = get_posts('numberposts=-1');
						
						$ibizmag_featured_1 = get_option('ibizmag_featured_1');

						?>
						<select name="ibizmag_featured_1" id="ibizmag_featured_1">
                        <option value="0">[Select Post]</option>
						<?php
						foreach($myposts as $post)
						{
							$post_id = $post->ID;
							if($post_id == $ibizmag_featured_1)
							{
							?>
							 <option value="<?php echo $post_id; ?>" selected="selected"><?php the_title(); ?></option>    
							<?php
							}
							else
							{
							?>
							 <option value="<?php echo $post->ID; ?>"><?php the_title(); ?></option>    
							<?php
							}
						}
						?>
						</select>
                   	</td>
                </tr>
                <tr valign="top">
                    <th scope="row">Featured Post #2</th>
                    <td>
                    	<?php 
						global $post;
						$myposts = get_posts('numberposts=-1');
						
						$ibizmag_featured_2 = get_option('ibizmag_featured_2');

						?>
						<select name="ibizmag_featured_2" id="ibizmag_featured_2">
                        <option value="0">[Select Post]</option>
						<?php
						foreach($myposts as $post)
						{
							$post_id = $post->ID;
							if($post_id == $ibizmag_featured_2)
							{
							?>
							 <option value="<?php echo $post_id; ?>" selected="selected"><?php the_title(); ?></option>    
							<?php
							}
							else
							{
							?>
							 <option value="<?php echo $post->ID; ?>"><?php the_title(); ?></option>    
							<?php
							}
						}
						?>
						</select>
                   	</td>
                </tr>
                <tr valign="top">
                    <th scope="row">Featured Post #3</th>
                    <td>
                    	<?php 
						global $post;
						$myposts = get_posts('numberposts=-1');
						
						$ibizmag_featured_3 = get_option('ibizmag_featured_3');

						?>
						<select name="ibizmag_featured_3" id="ibizmag_featured_3">
                        <option value="0">[Select Post]</option>
						<?php
						foreach($myposts as $post)
						{
							$post_id = $post->ID;
							if($post_id == $ibizmag_featured_3)
							{
							?>
							 <option value="<?php echo $post_id; ?>" selected="selected"><?php the_title(); ?></option>    
							<?php
							}
							else
							{
							?>
							 <option value="<?php echo $post->ID; ?>"><?php the_title(); ?></option>    
							<?php
							}
						}
						?>
						</select>
                   	</td>
                </tr>
                <tr valign="top">
                    <th scope="row">Featured Post #4</th>
                    <td>
                    	<?php 
						global $post;
						$myposts = get_posts('numberposts=-1');
						
						$ibizmag_featured_4 = get_option('ibizmag_featured_4');

						?>
						<select name="ibizmag_featured_4" id="ibizmag_featured_4">
                        <option value="0">[Select Post]</option>
						<?php
						foreach($myposts as $post)
						{
							$post_id = $post->ID;
							if($post_id == $ibizmag_featured_4)
							{
							?>
							 <option value="<?php echo $post_id; ?>" selected="selected"><?php the_title(); ?></option>    
							<?php
							}
							else
							{
							?>
							 <option value="<?php echo $post->ID; ?>"><?php the_title(); ?></option>    
							<?php
							}
						}
						?>
						</select>
                   	</td>
                </tr>
                <tr valign="top">
                    <th scope="row">Featured Post #5</th>
                    <td>
                    	<?php 
						global $post;
						$myposts = get_posts('numberposts=-1');
						
						$ibizmag_featured_5 = get_option('ibizmag_featured_5');

						?>
						<select name="ibizmag_featured_5" id="ibizmag_featured_5">
                        <option value="0">[Select Post]</option>
						<?php
						foreach($myposts as $post)
						{
							$post_id = $post->ID;
							if($post_id == $ibizmag_featured_5)
							{
							?>
							 <option value="<?php echo $post_id; ?>" selected="selected"><?php the_title(); ?></option>    
							<?php
							}
							else
							{
							?>
							 <option value="<?php echo $post->ID; ?>"><?php the_title(); ?></option>    
							<?php
							}
						}
						?>
						</select>
                   	</td>
                </tr>
            </table>
            
            <h4>Featured Categories</h4>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">Category #1</th>
                    <td>
                    	<select name="ibizmag_cat_1" id="ibizmag_cat_1"> 
                         <option value="0">[Select Category]</option> 
                         <?php 
						  $ibizmag_cat_1 = get_option('ibizmag_cat_1');
                          
						  $categories = get_categories(''); 
                          foreach ($categories as $cat) {
						  	$term_id = $cat->term_id;
                            $option = '<option value="'.$term_id.'"';
							
							if($term_id == $ibizmag_cat_1)
							{
								$option .= ' selected>';
							}
							else
							{
								$option .= '>';
							}
	
                            $option .= $cat->cat_name;
                            $option .= '</option>';
                            echo $option;
                          }
                         ?>
                        </select>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">Category #2</th>
                    <td>
                    	<select name="ibizmag_cat_2" id="ibizmag_cat_2"> 
                         <option value="0">[Select Category]</option> 
                         <?php 
						  $ibizmag_cat_2 = get_option('ibizmag_cat_2');
                          
						  $categories = get_categories(''); 
                          foreach ($categories as $cat) {
						  	$term_id = $cat->term_id;
                            $option = '<option value="'.$term_id.'"';
							
							if($term_id == $ibizmag_cat_2)
							{
								$option .= ' selected>';
							}
							else
							{
								$option .= '>';
							}
	
                            $option .= $cat->cat_name;
                            $option .= '</option>';
                            echo $option;
                          }
                         ?>
                        </select>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">Category #3</th>
                    <td>
                    	<select name="ibizmag_cat_3" id="ibizmag_cat_3"> 
                         <option value="0">[Select Category]</option> 
                         <?php 
						  $ibizmag_cat_3 = get_option('ibizmag_cat_3');
                          
						  $categories = get_categories(''); 
                          foreach ($categories as $cat) {
						  	$term_id = $cat->term_id;
                            $option = '<option value="'.$term_id.'"';
							
							if($term_id == $ibizmag_cat_3)
							{
								$option .= ' selected>';
							}
							else
							{
								$option .= '>';
							}
	
                            $option .= $cat->cat_name;
                            $option .= '</option>';
                            echo $option;
                          }
                         ?>
                        </select>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">Category #4</th>
                    <td>
                    	<select name="ibizmag_cat_4" id="ibizmag_cat_4"> 
                         <option value="0">[Select Category]</option> 
                         <?php 
						  $ibizmag_cat_4 = get_option('ibizmag_cat_4');
                          
						  $categories = get_categories(''); 
                          foreach ($categories as $cat) {
						  	$term_id = $cat->term_id;
                            $option = '<option value="'.$term_id.'"';
							
							if($term_id == $ibizmag_cat_4)
							{
								$option .= ' selected>';
							}
							else
							{
								$option .= '>';
							}
	
                            $option .= $cat->cat_name;
                            $option .= '</option>';
                            echo $option;
                          }
                         ?>
                        </select>
                    </td>
                </tr>
            </table>
        
            <input type="hidden" name="action" value="update" />
            <input type="hidden" name="page_options" value="ibiz_mag_feedburner_id, ibiz_mag_feedburner_url, ibiz_mag_about_us, ibizmag_video_cat, ibizmag_featured_1, ibizmag_featured_2, ibizmag_featured_3, ibizmag_featured_4, ibizmag_featured_5, ibizmag_cat_1, ibizmag_cat_2, ibizmag_cat_3, ibizmag_cat_4" />
            <p class="submit">
                <input type="submit" name="SaveThemeSetting" id="SaveThemeSetting" value="<?php _e('Save Theme Settings') ?>" />
            </p>
        </form>
    </div>

    <?php
}
?>